import 'package:amap_flutter_base/amap_flutter_base.dart';
import 'package:amap_flutter_map/amap_flutter_map.dart';

import '../utils/Constant.dart';
import '../utils/Utils.dart';

class AMap {
  static AMap? _instance;

  AMap._();

  static AMap get instance => _instance ??= AMap._();

  late AMapController _mapController;
  double zoom = 18;
  final Map<String, Marker> _marker = <String, Marker>{};
  LatLng lng = const LatLng(22.578404, 113.910566);

  //高德地图
  AMapWidget aMapWidget() {
    _addMarker(lng);
    return AMapWidget(
      apiKey: Constant.amapApiKeys,
      privacyStatement: Constant.amapPrivacyStatement,
      onMapCreated: _onMapCreated,
      mapType: MapType.normal,
      compassEnabled: false,
      buildingsEnabled: true,
      scaleEnabled: false,
      markers: Set<Marker>.of(_marker.values),
      onCameraMoveEnd: (cameraPosition) {
        zoom = cameraPosition.zoom;
      },
    );
  }

  //地图创建完成
  void _onMapCreated(AMapController controller) {
    _mapController = controller;
  }

  // 进行位置移动
  void changeCameraPosition(LatLng lng) {
    if (_mapController != null) {
      _mapController.moveCamera(
        CameraUpdate.newCameraPosition(
          CameraPosition(target: lng, zoom: zoom, tilt: 30, bearing: 0),
        ),
        animated: true,
      );
    }
    _addMarker(lng);
  }

  //绘制覆盖物
  void _addMarker(LatLng lng) {
    _marker.clear();
    Marker marker = Marker(
        position: lng,
        icon: BitmapDescriptor.fromIconPath(
            Utils.instance.getImagePathPNG("map_marker_icon")),
        rotation: 90);
    _marker[marker.id] = marker;
  }
}
